package com.yenlien.traceyun.business.trace;

import com.yenlien.traceyun.common.utils.LoggerUtil;
import com.yenlien.traceyun.webase.config.ApplicationContextConfig;
import com.yenlien.traceyun.webase.dao.BaseJpaDao;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * 追溯码管理
 *
 * @author master yi
 */
public class TraceManger {

    private ExecutorService executorService = Executors.newFixedThreadPool(5);

    private static TraceManger codeManger = new TraceManger();


    private TraceManger() {
    }

    ;

    public static TraceManger getInstance() {
        return codeManger;
    }

    /**
     * 创建人物
     *
     * @param traceService
     * @param code
     * @param endStep
     */
    public void createJob(TraceService traceService, String code, Object endStep) {
        if (endStep.getClass().isAnnotationPresent(Trace.class)) {
            String dao = endStep.getClass().getName().replace("entity", "dao") + "Dao";
            try {
                BaseJpaDao baseJpaDao = (BaseJpaDao) ApplicationContextConfig.getBean(Class.forName(dao));
                this.subJob(new TraceJob(traceService, code, endStep));
            } catch (ClassNotFoundException e) {
                LoggerUtil.getLogger().error("添加任务失败，没有找到dao：" + dao);
            }

        } else {
            LoggerUtil.getLogger().error("添加任务失败，没有Trace注解");
        }

    }

    /**
     * 提交任务
     *
     * @param job
     * @return
     */
    public void subJob(TraceJob job) {
        executorService.submit(job);
    }

}
